package com.fanli.udf;

import org.apache.hadoop.hive.ql.exec.UDF;

/**
 * Created by liangdong.wu on 2016/8/23.
 */
public class AbtestParser extends UDF {
    public String evaluate(String abtest,String large,String small) {
        if(abtest.trim() != "" && !abtest.isEmpty()){
            String sub_abtest = "";
            int last_test = 0;
            for(String var:abtest.split(large)){
                String[] group_arr = var.split(small);
                if(group_arr.length == 2) {
                    String groups = group_arr[1];
                    try{
                        last_test += Integer.parseInt(group_arr[0]);
                    }catch(NumberFormatException e){
                        System.out.println("数字转换错误");
                        e.printStackTrace();
                    }
                    sub_abtest += Integer.toString(last_test) + '=' + groups + "&";
                }

            }
//            return sub_abtest.substring(0,sub_abtest.length()-1);
            return sub_abtest;
        }
        return null;

    }
    public static void main(String[] args) {
        AbtestParser abtestParser = new AbtestParser();
        String str = abtestParser.evaluate("17453_b-1_b-32579_b-72_d-138_b-112_b-6_b-9728_a-e7a0","-","_");
        System.out.println(str);
    }
}
